//
//  FLMChatViewController.m
//  FLMSuperS
//
//  Created by chenshan on 2016/10/9.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMChatViewController.h"
#import "FLMChatTextCell.h"
#import "FLMChatBottomView.h"
#import "FLMMessageModel.h"
#import "IQKeyboardManager.h"

CGFloat kFLMChatBottomViewHeight = 50.0;

@interface FLMChatViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,FLMChatBottomDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *messages;
@property (nonatomic, strong) FLMChatBottomView *bottomView;

@end

@implementation FLMChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = NO;
    
    self.view.backgroundColor = FLMColorBackgroud;
    self.title = @"超级买家消息中心";
    
    
    
    FLMMessageModel *model1 = [[FLMMessageModel alloc] initWithDict:@{
                                                                      @"content" :@"fanly your fanly your fanly yourfanly your fanly yourfanly yourfanly yourfanly yourfanly",                                                                     @"time" : @"12:59"
                                                                      }];
    
    FLMMessageModel *model2 = [[FLMMessageModel alloc] initWithDict:@{
                                                                      @"content" :@"fanly your",                                                                     @"time" : @"12:59"
                                                                      }];
    
    _messages = @[].mutableCopy;
    
    [_messages addObject:model1];
    [_messages addObject:@"12:59"];
    [_messages addObject:model2];
    [_messages addObject:model2];
    
    

    _bottomView = [[FLMChatBottomView alloc] initWithFrame:CGRectMake(0, self.view.height - kFLMChatBottomViewHeight, self.view.width, kFLMChatBottomViewHeight)];
    _bottomView.delegate = self;
    _bottomView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
    
    [self.view addSubview:_bottomView];
    
    [self.view addSubview:self.tableView];
}

- (void)didChangeFrameToHeight:(CGFloat)toHeight
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.tableView.frame;
        rect.origin.y = NAVIGATIONBAR_HEIGHT;
        rect.size.height = self.tableView.height - toHeight;

        self.tableView.frame = rect;
    }];

    if (self.tableView.contentSize.height > self.tableView.frame.size.height) {
        CGPoint offset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
        [self.tableView setContentOffset:offset animated:YES];
    }
}

- (void)chatBottomViewDidSendMessage:(NSString *)message {
    [self sendMessage];
}

- (void)chatBottomViewDidEndSend {
}

- (void)sendMessage {
    FLMMessageModel *model = [[FLMMessageModel alloc] initWithDict:@{
                                                                      @"content" :@"fafafafafafafasfasfasff shshshsh ",                                                                     @"time" : @"12:59"
                                                                      }];
    [_messages addObject:model];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        
        if (self.tableView.contentSize.height > self.tableView.frame.size.height) {
            CGPoint offset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
            offset.y -= NAVIGATIONBAR_HEIGHT ;
            [self.tableView setContentOffset:offset animated:YES];
        }
    });
}

#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row < _messages.count) {
        id object = _messages[indexPath.row];
        
        if([object isKindOfClass:[NSString class]]) {
            UITableViewCell *timeCell = [tableView dequeueReusableCellWithIdentifier:@"TimeCell" forIndexPath:indexPath];
            timeCell.backgroundColor = [UIColor clearColor];
            timeCell.selectionStyle = UITableViewCellSelectionStyleNone;
            timeCell.textLabel.textAlignment = NSTextAlignmentCenter;
            timeCell.textLabel.font = [UIFont systemFontOfSize:10.0];
            timeCell.textLabel.text = (NSString *)object;
            
            return timeCell;
        }
        else {
            FLMChatTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMChatTextCell" forIndexPath:indexPath];
            
            cell.model = _messages[indexPath.row];
            
            cell.indexPath = indexPath;
            cell.userType = indexPath.row % 2 ? FLMChatCellUserTypeMyself : FLMChatCellUserTypeOther;
            
            if (cell.userType == FLMChatCellUserTypeMyself) {
                cell.avatarImgView.image = [UIImage imageNamed:@"2"];
            }
            else {
                cell.avatarImgView.image = [UIImage imageNamed:@"2"];
            }
            
            return cell;
        }
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id object = _messages[indexPath.row];
    
    if ([object isKindOfClass:[NSString class]]) {
        return 40;
    }
    else {
        FLMChatTextCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FLMChatTextCell class])];
        cell.indexPath = indexPath;
        cell.model = _messages[indexPath.row];
        cell.userType = FLMChatCellUserTypeMyself;
        
        if (cell.userType == FLMChatCellUserTypeMyself) {
            cell.avatarImgView.image = [UIImage imageNamed:@"2"];
        }
        else {
            cell.avatarImgView.image = [UIImage imageNamed:@"2"];
        }
        
        return [cell getHeight];
    }
}

#pragma mark - Getters

- (UITableView *)tableView {
    if(!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, ScreenW, ScreenH - kFLMChatBottomViewHeight - NAVIGATIONBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = FLMColorBackgroud;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.tableFooterView = [[UIView alloc] init];
        [_tableView registerClass:[FLMChatTextCell class] forCellReuseIdentifier:@"FLMChatTextCell"];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"TimeCell"];
    }
    return _tableView;
}

@end
